package com.pp.tourism.web.controller;

import com.pp.tourism.web.dto.ApiResponse;
import com.pp.tourism.web.exception.ApiException;
import com.pp.tourism.web.exception.ApiRtnCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一错误输出
 * <p>
 */
@Slf4j
@ControllerAdvice
public class TwExceptionController {

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public ApiResponse appErrorHandler(ApiException e) {
        return ApiResponse.error(e.getCode());
    }

    @ResponseBody
    @ExceptionHandler(value = ServletRequestBindingException.class)
    public ApiResponse needLoginErrorHandler(ServletRequestBindingException e) {
        return ApiResponse.error(ApiRtnCode.NEED_LOGIN);
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ApiResponse unexpectedErrorHandler(Exception e) {
        log.error("unexpectedError", e);
        return ApiResponse.error(ApiRtnCode.UNEXPECTED_ERROR);
    }
}
